/**
 * kl yy-ds @2021
 */
package com.cxps.server.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxps.cloud.message.ApiResponse;
import com.cxps.server.user.domain.UserForm;
import com.cxps.server.user.domain.UserQuery;
import com.cxps.server.user.domain.UserVo;
import com.cxps.server.user.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制层
 *
 * @author YouCai.Liu
 * @since 2021/10/4
 * ...
 */
@Slf4j
@RestController
@RequestMapping("/admin/user")
@Api(tags = "用户API")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户列表
     *
     * @param query
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "用户列表", response = ApiResponse.class)
    public ApiResponse<IPage> getUserList(@ApiParam(name = "query", value = "检索条件")
                                          @RequestBody UserQuery query) {
        try {
            return ApiResponse.fireSuccess(userService.getUserList(query));
        } catch (IllegalArgumentException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.fireError();
        }
    }

    /**
     * 添加用户
     *
     * @param form
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加用户", response = ApiResponse.class)
    public ApiResponse addUser(@ApiParam(name = "form", value = "用户表单对象")
                               @RequestBody UserForm form) {
        try {
            Assert.notNull(form, "form can not be null");
            userService.addUser(form);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            log.error("用户添加失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 根据ID查询用户
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据ID查询用户", response = ApiResponse.class)
    public ApiResponse<UserVo> get(@ApiParam(name = "id", value = "用户ID", required = true)
                                   @PathVariable("id") String id) {
        return ApiResponse.fireSuccess(userService.getUserById(id));
    }

    /**
     * 修改用户
     *
     * @param form
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改用户", response = ApiResponse.class)
    public ApiResponse updateUser(@ApiParam(name = "form", value = "用户表单对象")
                                  @RequestBody UserForm form) {
        userService.updateUser(form);
        return ApiResponse.fireSuccess();
    }

    /**
     * 根据用户ID删除用户
     *
     * @param id
     * @return
     */
    @GetMapping("/del/{id}")
    @ApiOperation(value = "单个删除", response = ApiResponse.class)
    public ApiResponse delUserById(@ApiParam(name = "id", value = "用户ID", required = true)
                                   @PathVariable("id") String id) {
        userService.delUserById(id);
        return ApiResponse.fireSuccess();
    }

    /**
     * 根据用户ID集合批量删除用户
     *
     * @param ids
     * @return
     */
    @GetMapping("/del2")
    @ApiOperation(value = "批量删除", response = ApiResponse.class)
    public ApiResponse delUserById(@ApiParam(name = "ids", value = "用户ID集合", required = true)
                                   @RequestParam("ids") String[] ids) {
        try {
            Assert.notEmpty(ids, "ids can not be empty！");
            userService.delUserByIds(ids);
        } catch (IllegalArgumentException e) {
            return ApiResponse.fireError(e.getMessage());
        }
        return ApiResponse.fireSuccess();
    }

    /**
     * 添加角色
     *
     * @param id
     * @param roles
     * @return
     */
    @GetMapping("/addRole")
    @ApiOperation(value = "添加角色", response = ApiResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "roles", value = "角色ID集合", required = true, dataType = "array", paramType = "query")
    })
    public ApiResponse addRole(@RequestParam("id") String id, @RequestParam("roles") String[] roles) {
        try {
            Assert.hasLength(id, "id can not be empty！");
            Assert.notEmpty(roles, "roles can not be empty！");
            userService.addRole(id, roles);
        } catch (IllegalArgumentException e) {
            return ApiResponse.fireError(e.getMessage());
        }
        return ApiResponse.fireSuccess();
    }

    /**
     * 根据用户ID查询用户角色ID
     *
     * @param id
     * @return
     */
    @GetMapping("/getRole/{id}")
    @ApiOperation(value = "根据用户ID查询用户角色ID", response = ApiResponse.class)
    public ApiResponse getUserRoleIdByUserId(@ApiParam(name = "id", value = "用户ID", required = true)
                                             @PathVariable("id") String id) {
        return ApiResponse.fireSuccess(userService.getUserRoleIdByUserId(id));
    }

}
